import {
  queryPageList,
  deleteByIds,
  queryResAuditTaskHistoryById,
  queryTaskHistoryCount,
} from '@/services/sensitiveData/taskHistory';

export default {
  namespace: 'taskHistory',
  state: {
    dataqueryPageList: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },
    dataqueryTaskHistoryCount: {
      leList: [],
      leTypeList: [],
      leWordeList: [],
    },
  },

  effects: {
    *queryPageList({ payload }, { call, put }) {
      const response = yield call(queryPageList, payload);
      yield put({
        type: 'savequeryPageList',
        payload: response.data || {
          dataList: [],
          pageNum: 0,
          pageSize: 0,
          totalCount: 0,
        },
      });
    },
    *queryListById({ payload }, { call, put }) {
      const response = yield call(queryResAuditTaskHistoryById, payload);
      yield put({
        type: 'saveNewList',
        payload: response.data || [],
      });
    },
    *deleteByIds({ payload }, { call, put }) {
      const response = yield call(deleteByIds, payload);
      return response;
    },
    *queryTaskHistoryCount({ payload }, { call, put }) {
      const response = yield call(queryTaskHistoryCount, payload);
      yield put({
        type: 'savequeryTaskHistoryCount',
        payload: response.data || {
          leTypeList: [],
          leWordeList: [],
          leList: [],
        },
      });
    },
  },
  reducers: {
    savequeryPageList(state, { payload }) {
      return {
        ...state,
        dataqueryPageList: payload,
      };
    },
    saveNewList(state, action) {
      const data = state.dataqueryPageList.dataList;
      const replaceData = action.payload;
      const arr = [];
      for (let i = 0; i < data.length; i++) {
        if (replaceData.some(item => item.historyId == data[i].historyId)) {
          arr.push(replaceData.find(item => item.historyId == data[i].historyId));
        } else {
          arr.push(data[i]);
        }
      }
      return {
        ...state,
        dataqueryPageList: {
          dataList: arr,
          pageNum: state.dataqueryPageList.pageNum,
          pageSize: state.dataqueryPageList.pageSize,
          totalCount: state.dataqueryPageList.totalCount,
        },
      };
    },
    savequeryTaskHistoryCount(state, { payload }) {
      return {
        ...state,
        dataqueryTaskHistoryCount: payload,
      };
    },
  },
};
